import { Injectable } from "@nestjs/common";
import { PassportStrategy } from '@nestjs/passport'
import { Strategy } from "passport-local";
import { AuthService } from "./auth.service";

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {

    constructor(
        private readonly authService: AuthService
    ) {
        console.log("local strategy init");
        super({
            usernameField: 'username',
            passwordField: 'password',
        });


        
    }
    // 这个方法会从你的 请求参数中 拿到用户名和密码，一定要传递请求参数！！！！
    async validate(username: string, password: string): Promise<any> {

        console.log("local strategy ts enter", username, password);

        const user = await this.authService.validateUser(username, password);
        // if (!user) {
        //     throw new UnauthorizedException('用户名或密码不正确！');
        // }
        console.log("local strategy ts leave", user);
        // 最终结果是在 在请求对象上创建user 属性 等价于res.user = user
        return user;
    }
}
